package com.ltu.model.request;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ltu.enums.RedisBusinessDict;
import com.ltu.util.exception.ApiException;
import com.ltu.util.redis.RedistUtil;

import lombok.extern.slf4j.Slf4j;

/**  
 * @Description: 订单回滚从redis获取到的key拼接规则：产品类型_出售类型_订单Id example:CardOrder_1_232,表示卡项订单、常规出售、订单id为232。
 * @author: 若尘  
 * @date 2019年7月7日 下午2:09:23
 * @version V1.0  
 */
@Slf4j
@Component
public class RollBackKey {

	@Autowired
	RedistUtil redistUtil;
	public  void setOrderKey(RedisBusinessDict.BusinessName rollBackKey,Long id){
		if(id==null || id.intValue()==0){throw new ApiException("id不能为空！");}
		StringBuilder  key=new StringBuilder("RollBackKey");
		key.append(':');
		key.append(rollBackKey.getDesc());
		key.append(':');
		key.append(rollBackKey.getValue());
		key.append(':');
		key.append(id.toString());
		String orderKey= key.toString();
		if(!redistUtil.hasKey(orderKey)){
			redistUtil.setValue(orderKey, 1, new Long(60*30));
		}
	}
	/**
	 * @date 2019年7月13日
	 * @author 若尘
	 * @Description 需要加入到redis队列监听key过期的方法
	 * @param rollBackKey
	 * @param id
	 * @param expireTime 过期时间 s 单位
	 * void
	 */
	public  void setOrderKey(RedisBusinessDict.BusinessName rollBackKey,Long id,Long expireTime){
		if(id==null || id.intValue()==0){throw new ApiException("id不能为空！");}
		StringBuilder  key=new StringBuilder("RollBackKey");
		key.append(':');
		key.append(rollBackKey.getDesc());
		key.append(':');
		key.append(rollBackKey.getValue());
		key.append(':');
		key.append(id.toString());
		String orderKey= key.toString();
		if(!redistUtil.hasKey(orderKey)){
			redistUtil.setValue(orderKey, 1, expireTime);
		}
	}
	public  void setOrderKey(RedisBusinessDict.BusinessName rollBackKey,String keyValue,Long expireTime){
		StringBuilder  key=new StringBuilder("RollBackKey");
		key.append(':');
		key.append(rollBackKey.getDesc());
		key.append(':');
		key.append(rollBackKey.getValue());
		key.append(':');
		key.append(keyValue);
		String orderKey= key.toString();
		redistUtil.setValue(orderKey, 1, expireTime);
	}
	
	public void removerOrderKey(RedisBusinessDict.BusinessName rollBackKey,Long id){
		if(id==null || id.intValue()==0){throw new ApiException("id不能为空！");}
		StringBuilder  key=new StringBuilder("RollBackKey");
		key.append(':');
		key.append(rollBackKey.getDesc());
		key.append(':');
		key.append(rollBackKey.getValue());
		key.append(':');
		key.append(id.toString());
		String orderKey= key.toString();
		redistUtil.removeKey(orderKey);
	}
	
	
	
	/**
	 * 
	 * @date 2019年7月7日
	 * @author 若尘
	 * @Description
	 * @param rollBackKeyArr长度4 每个下标对应如下含义：0：redis中需要处理业务的key类型字符，1：对应订单的字符，2对应出售类型，3对应的订单id
	 * @return
	 * RedisRollbackObject
	 */
	public static RedisRollbackObj getRollbackObj(String []  rollBackKeyArr){
		if(rollBackKeyArr==null || rollBackKeyArr.length!=4){
			log.error("数组为空或者长度不对",rollBackKeyArr.toString());
		}
		RedisRollbackObj obj= new RedisRollbackObj();
		obj.setTingType(rollBackKeyArr[1]);
		obj.setSaleType(Integer.parseInt( rollBackKeyArr[2]));
		obj.setThingId( rollBackKeyArr[3]);
		return obj;
		
	}
	
	
}
